<?php

namespace app\admin\model\content;

use think\Model;


class Content_notice extends Model
{

    

    

    // 表名
    protected $name = 'content_notice';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'page_type_text',
        'start_time_text',
        'end_time_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getPageTypeList()
    {
        return ['1' => __('Page_type 1'), '2' => __('Page_type 2')];
    }


    public function getPageTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['page_type']) ? $data['page_type'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getPageTypeList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPageTypeAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
